﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using MySqlX.XDevAPI.Common;
using Newtonsoft.Json;
using SqlSugar.Extensions;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using static CoreShop.Library.GlobalEnumVars;

namespace CoreShop.App
{
    public partial class frmFoodPresent : UIForm
    {
        private List<corecmsfoodtag> tagList = null;
        private corecmsfoodordering orderingSelected = null;
        private List<corecmsfoodordering> orderingDetail = new List<corecmsfoodordering>();

        public frmFoodPresent(corecmsfoodordering _orderingSelected)
        {
            orderingSelected = _orderingSelected;
            orderingDetail.Add(_orderingSelected);
            InitializeComponent();
        }

        private void frmFoodRefund_Shown(object sender, System.EventArgs e)
        {
            BindOrdering();
        }

        private string GetMenuItemName(long orderingId)
        {
            return "uiMenuItem_" + orderingId.ToString();
        }

        private void BindOrdering()
        {
            try
            {
                layOrdering.Visible = false;

                //已赠、套餐内品、未下单的行不可赠。
                var orderingPresents = orderingDetail.Where(p => p.dataType != (int)GlobalEnumVars.OrderingDataType.Refund && p.foodPackId == 0).ToList();                

                layOrdering.Clear();
                foreach (var item in orderingPresents)
                {
                    //计算可赠商品

                    if (item.quantity > 0)
                    {
                        item.canRefundQty = item.quantity;
                        layOrderingRow(item);
                    }
                }
            }
            catch (Exception ex)
            {
                UIMessageBox.Show(ex.Message, "系统出错-BindOrdering:", UIStyle.Green, UIMessageBoxButtons.OK);
            }
            finally
            {
                layOrdering.Visible = true;
            }
        }

        private void layOrderingRow(corecmsfoodordering item)
        {
            if (item.quantity > 1)
            {
                UIReturnItem2 item2 = new UIReturnItem2();
                item2.Tag = "2";
                item2.Name = GetMenuItemName(item.id);
                item2.DishName = item.name;
                item2.SpecName = item.specName;
                item2.Number = CommFunc.FormatDecimal(item.quantity);
                item2.MaxNumber = item.quantity;
                item2.Text = "1";
                item2.Amount = item.allAmount.ToString();
                item2.Price = item.realPrice.ToString("#0.0#")+"/"+item.unitName;
                layOrdering.Add(item2);
            }
            else
            {
                UIReturnItem2 item1 = new UIReturnItem2();
                item1.Name = GetMenuItemName(item.id);
                item1.Tag = "1";
                item1.DishName = item.name;
                item1.SpecName = item.specName;
                item1.Number = CommFunc.FormatDecimal(item.quantity);
                item1.MaxNumber = item.quantity;
                item1.Price = item.realPrice.ToString("#0.0#") + "/" + item.unitName;
                layOrdering.Add(item1);
            }
        }

        private void uiButton3_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void frmFoodRefund_Load(object sender, System.EventArgs e)
        {

        }

        private void btnRefundAll_Click(object sender, EventArgs e)
        {
            List<OrderingQuantity> refundList = new List<OrderingQuantity>();
            string msg = "";
            foreach (var item in orderingDetail)
            {
                var mControl = layOrdering.Get(GetMenuItemName(item.id));
                if (mControl == null) continue;
                decimal refundQuantity = 0;
                var mTag = mControl.Tag.ObjToInt(1);
                var menu2 = (UIReturnItem2)mControl;
                refundQuantity = menu2.RefundQuantity.ObjToDecimal(0);
                if (refundQuantity == 0) continue;

                if ((refundQuantity > item.quantity) || (refundQuantity > item.canRefundQty))
                {
                    msg = string.Format("第{0}行【{1}】的赠送菜数量大于可赠送数据，保存失败。", item.rowId, item.name);
                    UIMessageBox.Show(msg, "出错:", UIStyle.Green, UIMessageBoxButtons.OK);
                    return;
                }
                var refundModel = new OrderingQuantity();
                refundModel.id = item.id;
                refundModel.orderId = item.orderId;
                refundModel.quantity = refundQuantity;
                refundList.Add(refundModel);
            }
            if ((refundList == null) || (refundList.Count == 0))
            {
                msg = "没有选择赠送数据，无法保存。";
                UIMessageBox.Show(msg, "提示:", UIStyle.Green, UIMessageBoxButtons.OK);
                return;
            }

            var regModel = new FoodRefundReqModel();
            regModel.items = refundList;
            regModel.billerId = AppConstVars.sysUser.id;
            regModel.billerName = AppConstVars.sysUser.nickName;

            var orderingService = new corecmsfoodorderingServices();
            var bl = orderingService.SetFoodPresent(regModel, ref msg);
            if (!bl)
            {
                UIMessageBox.Show(msg, "提示:", UIStyle.Green, UIMessageBoxButtons.OK);
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

    }
}
